package com.etrans.web.gps.business.report.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.etrans.web.gps.base.controller.BaseController;
import com.etrans.web.gps.business.report.service.EnterpriseStatisticsService;
import com.etrans.web.gps.business.system.service.CommonService;
import com.etrans.web.gps.business.system.service.ExportWordService;
import com.etrans.web.gps.util.DateUtil;

/**
 * 报警综合分析
 * @author jw.chen
 *
 */
@Controller
@RequestMapping("/report/entSta/*")
public class EnterpriseStatisticsController extends BaseController{
	
	private final static Logger logger = LoggerFactory.getLogger(AlarmSynAnalyController.class);
	
	@Autowired
	private EnterpriseStatisticsService entStaService;
	
	@Autowired
	public CommonService commonService;
	
	@Autowired
	private ExportWordService exportWord;
	/**
	 * 页面跳转入口
	 * @return
	 */
	@RequestMapping(value="getAlarmStatistics.html")
	public ModelAndView getAlarmSynAnalysis(){
		ModelAndView mv =new ModelAndView();
		mv.setViewName("views/business/report/enterpriseStatistics");
		return mv;
	}
	
	/**
	 * 查询
	 * 入网率/上线率/合格率/漂移率/回复率
	 * @param request
	 * @param response
	 * @param inputDate
	 * @param areaId
	 * @param type
	 */
	@RequestMapping("queryEnterpriseSwap.html")
	public void queryEnterpriseSwap(HttpServletRequest request,HttpServletResponse response,String inputDate,
			Integer areaId){
		try {
			if (areaId == null || areaId <= 0) {
				areaId= this.getSysUser().getArea_id();	
			}
			List<Integer> areaIdList  = commonService.queryAreaListById(areaId);
			Map<String, Object> result = entStaService.queryEnterpriseSwap(areaIdList,inputDate);
			this.ajaxResultJsonWithGzip(request, response, result==null?"":result);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
	}
	
	/**
	 * 查询完整率
	 * @param request
	 * @param response
	 * @param inputDate
	 * @param areaId
	 * @param type
	 */
	@RequestMapping("queryEnterpriseSwap2.html")
	public void queryEnterpriseSwap2(HttpServletRequest request,HttpServletResponse response,String inputDate,
			Integer areaId){
		try {
			if (areaId == null || areaId <= 0) {
				areaId= this.getSysUser().getArea_id();	
			}
			List<Integer> areaIdList  = commonService.queryAreaListById(areaId);
			List<Map<String, Object>> result = entStaService.queryEnterpriseSwap2(areaIdList,inputDate);
			this.ajaxResultJsonWithGzip(request, response, result==null?"":result);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
	}
	
	@RequestMapping(value = "exportTableData.html")
	public void exportTableData(HttpServletResponse response,HttpServletRequest request,String inputDate,
			Integer areaId){
		try {
			if (areaId == null || areaId <= 0) {
				areaId= this.getSysUser().getArea_id();	
			}
			List<Integer> areaIdList  = commonService.queryAreaListById(areaId);
			Map<String, Object> dataMap = entStaService.exportEnterpriseSwap(areaIdList,inputDate);
			String month = inputDate.split("-").length==2?inputDate.split("-")[1]:"0";
			dataMap.put("term", month);
			dataMap.put("reportDate", new SimpleDateFormat("yyyy年MM月dd日").format(new Date()));
			String area = entStaService.queryAreaNameById(areaId);
			String province = area.split("-").length==2?area.split("-")[0]:"";
			String city = area.split("-").length==2?area.split("-")[1]:"";
			dataMap.put("province", province);
			dataMap.put("city", city);
			dataMap.put("lastMonth", inputDate.replace("-", "年"));
			dataMap.put("lastDay", DateUtil.getLastDayOfMonth(Integer.valueOf(inputDate.split("-")[0]),Integer.valueOf(month)));
			String vm_name = "NetworkSpreadingMonth.xml";
			String fileName = entStaService.queryAreaNameById(areaId) + month + "月联网联控月报";
			exportWord.exportWord(response,dataMap,fileName,vm_name);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
	}
}
